<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../../css/login.css" />
</head>

<body>
    <div class="pos">
        <div class="login">
            <span class="close iconfont icon-close"></span>
            <img src="../../img/login/loginlogo.png" alt="">
            <ul>
                <li></li>
                <li class="phone">使用手机号登陆</li>
                <li></li>
            </ul>
            <!-- <form action="" id="form"> -->
            <input style="margin-top: 0;" type="text" class="tel" id="username" placeholder="请输入您的手机号">
            <input type="password" class="pass" id="password" placeholder="请输入密码">
            <span class="tips"></span>
            <button class="loginbtn">
                    登陆
                </button>
            <!-- </form> -->
            <p>
                <span>
                    还没有账号?
                    <a href="./register.html">点击注册</a>
                </span>
            </p>
        </div>
    </div>
    <!-- <script src="../../utils/utils.js"></script> -->

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.9.1/jquery.min.js
    "></script>
    <script src="../../utils/http.js"></script>
    <script>
        $('.loginbtn').on('click', async function() {
            // alert('ok')

            //做一些基本的验证
            //
            let username = $('#username').val()
            let password = $('#password').val()
            if (!username || !password) {
                $('.tips').html('用户名或密码不能为空')
                return
            }
            //用户名和密码长度，那就再加判断

            let {
                err,
                result
            } = await myAjax({
                url: '/api/login',
                method: 'post',
                data: {
                    username: $('#username').val(),
                    password: $('#password').val()
                }
            })

            if (result.status == 200) {
                alert('登录成功')
                    //token和mid需要存储在本地存储中。
                location.href = '../../index.html' //跳转到首页
                    // location.href = '/' //跳转到首页
                    //localStorage.setItem(key,value) //只能存储字符串
                let {
                    mid,
                    token
                } = result.result
                localStorage.setItem('mid', mid) //比如报名的时候要用
                localStorage.setItem('token', token) //有权限的接口要通过headers传递
                return
            }
            // alert( result.msg )
            $('.tips').html(result.msg)
        })
    </script>
</body>

</html>